package com.rzjm.api.netty;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.rzjm.api.util.DateUtil;
import lombok.Data;

import java.io.Serializable;

/**
 * @创建者：zhouwei
 * @创建时间：2021/5/24
 * @描述：
 */
@Data
public class MessageModel implements Serializable {

    private String header;

    private String order;

    private String clientNum;

    private String data;

    private String footer;

    private String msgData;

    /**  消息类型  0 - 设备鸡蛋箱格数据  1 - 蓝牙定位数据  */
    private Integer msgType;

    private JSONObject jsonData;

    public MessageModel(String msg){
//        System.out.println("接收到消息：" + msg +  "       " + DateUtil.getTime());
        splitMsg(msg);
    }

    //MS01202201101,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630|1,23552,774,00000001655773091838,9630,ME
    private void splitMsg(String msg){
        this.msgData = msg.toUpperCase();
        if(StrUtil.isNotBlank(msg)){
            if(msg.contains("MS")){
                this.msgType = 0;
                this.header = msg.substring(0, 2);
                this.order = msg.substring(2, 4);
                this.clientNum = msg.substring(4,12);

                int indexME = msg.lastIndexOf("ME");
                this.data = msg.substring(12,indexME);
                this.footer = msg.substring(indexME,indexME + 2);
            }
        }
    }
}
